﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;

namespace YDWin.Order
{
    public partial class AddDyeingForm : AddForm
    {
        OrderDetailsService svr = new OrderDetailsService();
        FactoryService fs = new FactoryService();
        SpecService ss = new SpecService();

        public V_Order editTu = new V_Order();
 
        public string state = "";
        public AddDyeingForm()
        {
            InitializeComponent();


            //设置日期控件格式
            this.deOperationTime.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.deOperationTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.deOperationTime.Properties.Mask.EditMask = "yyyy年MM月dd日";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            T_Order_Details fact = new T_Order_Details(); 
             
            fact.Factory_ID = (long)cboFactoryName.EditValue;
            fact.Factory_Name = cboFactoryName.Text;
            fact.OperatintTime = (DateTime)deOperationTime.EditValue;
            fact.Quantity = decimal.Parse(nudQuantity.Text);
            fact.Order_ID =editTu.ID;
            fact.StartWeight = decimal.Parse(nudStartWeight.Text);
            fact.ProcessUnitPrice = decimal.Parse(nudProcessUnitPrice.Text);
            fact.ProcessCost = decimal.Parse(nudProcessCost.Text);
            fact.Spec_Name = cboSpecName.Text;
            fact.Spec_ID = (long)cboSpecName.EditValue;
            fact.LossPer = decimal.Parse(txtLossPer.Text);

            double per = double.Parse(txtLossPer.Text) * 0.01;
            double weight = double.Parse(fact.StartWeight.ToString());
            fact.EndWeight =decimal.Parse( (weight * per).ToString());
            fact.OrderType =1; //
            
            //if (!state.Equals("") && state.Equals("Edit"))
            //{
            //   /// svr.EditItem(fact);
            //}
            //else
            //{

            //}
            svr.AddItem(fact);

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "磨毛信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddDyeingForm_Load(object sender, EventArgs e)
        {
            //厂家名称下拉框
            var fDs = fs.getComboxList(1);
            cboFactoryName.Properties.ValueMember = "ID";   //实际要用的字段
            cboFactoryName.Properties.DisplayMember = "FactoryName";    //要显示的字段
            cboFactoryName.Properties.DataSource = fDs;
            cboFactoryName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容
 
            deOperationTime.EditValue = DateTime.Now;
            nudQuantity.Text = ((decimal)editTu.SurplusQuantity).ToString();
            nudStartWeight.Text = ((decimal)editTu.SurplusWeight).ToString();

        }


        #region 数量录入后自动计算 
        private void nudQuantity_KeyUp(object sender, KeyEventArgs e)
        {
            //nudStartWeight.Text = (editTu. * decimal.Parse(nudQuantity.Text)).ToString();
        }

        private void nudStartWeight_KeyUp(object sender, KeyEventArgs e)
        {
            nudProcessCost.Text = (decimal.Parse(nudStartWeight.Text) * decimal.Parse(nudProcessUnitPrice.Text)).ToString();
        }

        private void nudProcessUnitPrice_KeyUp(object sender, KeyEventArgs e)
        {
            nudProcessCost.Text = (decimal.Parse(nudStartWeight.Text) * decimal.Parse(nudProcessUnitPrice.Text)).ToString();
        } 

        #endregion


        private void cboFactoryName_EditValueChanged(object sender, EventArgs e)
        {
            long factoryID = (long)cboFactoryName.EditValue;

            //磨毛下拉框
            var sDs = ss.getComboxList(factoryID,1); //磨毛
            cboSpecName.Properties.ValueMember = "ID";   //实际要用的字段
            cboSpecName.Properties.DisplayMember = "SpecName";    //要显示的字段
            cboSpecName.Properties.DataSource = sDs;
            cboSpecName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容

        }

 
    }
}